package com.liuwei.spring.cloud.common.utils.feishu.message;/**
 * @author : MarkdownMessage
 * @version V1.0
 * @date: 2023/5/24 6:08 e下午
 * @Description: TODO
 */

import com.fasterxml.jackson.annotation.JsonProperty;
import com.liuwei.spring.cloud.common.utils.feishu.element.Elements;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : MarkdownMessage
 * @version V1.0
 * @date: 2023/5/24 6:08 下午
 * @Description: TODO
 */
@Data
public class CardMessage extends BaseFeishuMessage {

    @JsonProperty("msg_type")
    private String messageType = "interactive";

    private Card card;

    @Data
    public static class Card{
        private MessageHeader header;
        private List<Elements.BaseFeishuElement> elements;
    }

    @Data
    public static class MessageHeader{
        private Elements.MdElement title;
    }

    public static CardMessage buildCardMessage(String mdTitle, String mdContent,String buttonText,String buttonHref){
        CardMessage message = new CardMessage();

        Card card = new Card();
        message.setCard(card);

        MessageHeader header = new MessageHeader();
        header.setTitle(new Elements.MdElement(mdTitle));
        card.setHeader(header);

        List<Elements.BaseFeishuElement> elements = new ArrayList<>();
        card.setElements(elements);

        // div
        Elements.DivElement div = new Elements.DivElement();
        div.setText(new Elements.MdElement(mdContent));
        elements.add(div);

        Elements.ActionElement act = new Elements.ActionElement();
        List<Elements.BaseFeishuElement> actions = new ArrayList<>();
        act.setActions(actions);
        elements.add(act);

        if(StringUtils.isNotEmpty(buttonText)){
            actions.add(new Elements.ButtonElement(buttonText,buttonHref));
        }
        return message;
    }
}
